package com.lanyuan.controller.index;

import com.baidu.aip.util.Base64Util;
import com.baidu.aip.util.Util;
import com.lanyuan.entity.app.TongueStandardFormMap;
import com.lanyuan.mapper.app.TongueStandardMapper;

import com.lanyuan.util.BaiduFaceAi;

import com.lanyuan.util.Result;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/")
public class ImageRecognitionController extends BaseController {
    @Autowired
    TongueStandardMapper tongueStandardMapper;

    @ResponseBody
    @RequestMapping(value = "getImageInfo",method = RequestMethod.POST)
    public Result getImageInfo(@RequestParam(value = "image") String image) throws Exception {
        //System.out.println(image);
//        Long id = BaiduImageSearch.similarSearch(image);
//        TongueStandardFormMap tongueStandardFormMap = tongueStandardMapper.findOne("id",id+"",TongueStandardFormMap.class);
        String id = BaiduFaceAi.faceSearch(image, "painting");
        if(StringUtils.isBlank(id)){
            return Result.error("识别失败");
        }
        TongueStandardFormMap tongueStandardFormMap = tongueStandardMapper.findOne("id",id,TongueStandardFormMap.class);
        return Result.ok().add("data",tongueStandardFormMap);
    }

    @ResponseBody
    @RequestMapping(value = "getRandomImage",method = RequestMethod.POST)
    public Result getRandomImage(@RequestParam(value = "id") String id){
        List<TongueStandardFormMap> list =tongueStandardMapper.randomListExcept( id);
        Random random = new Random();
        int r = random.nextInt(list.size());
        return Result.ok().add("data",list.get(r));
    }

    public static void main(String[] args) throws IOException {
        String f = "C:\\Users\\l\\Desktop\\lADPD3zULqmiPdrNAWLNAWI_354_354.jpg";
        byte[] bytes = Util.readFileByBytes(f);
        System.out.println(Base64Util.encode(bytes));
    }
}
